#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define PORT 7000
#define BUFFER_SIZE 1024
#define SERVERIP "127.0.0.1"

int main()
{
	int sock;
	struct sockaddr_in address;

    sock = socket(PF_INET, SOCK_STREAM, 0);
	memset(&address,0,sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr(SERVERIP);
	printf("%llu",inet_addr(SERVERIP));
    address.sin_port = htons(PORT);

    if(connect(sock, (struct sockaddr *)&address, sizeof(address)) == -1) {
        printf("连接服务器失败");
        exit(1);
    }

    /* 通过该套节字读写数据 */
	char sendbuf[BUFFER_SIZE];
    char recvbuf[BUFFER_SIZE];
    while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
	{
		send(sock, sendbuf, strlen(sendbuf), 0);
		if(strcmp(sendbuf,"exit\n")==0)
            break;
		recv(sock, recvbuf, sizeof(recvbuf), 0);
		printf("收到：\n");
		fputs(recvbuf, stdout);
		
		memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
	}

    /* 关闭套接字 */
    close(sock);
    exit(0);
}
